// 弹出树形选择弹出层，需要基于treeTable


function openDeptSelecter(state, id, pid, callBack) {
  layui.use(['layer','treeTable'], function () {
    var $ = layui.jquery;
    var treeTable = layui.treeTable;
    var layer=layui.layer;

    var tableSelectIns;
    selDeptIndex=layer.open({
			type:1,
			title:'选择部门',
			content:'<div style="padding:5px" ><table class="layui-hide" id="deptSelectTable" lay-filter="deptSelectTable"></table></div>',
			area:['700px','600px'],
			btn:['<span class="layui-icon layui-icon-ok"></span>确定'],
			yes:function(index){
				//得到选择的ID和名称
				var ckData=tableSelectIns.checkStatus();
				// console.log(ckData);
				callBack(ckData[0].id,ckData[0].title);
				layer.close(selDeptIndex);
			},
			success:function(index){
        tableSelectIns=treeTable.render({
          elem: '#deptSelectTable',
          cellMinWidth:true,
          tree: {
            iconIndex: 2,  // 折叠图标显示在第几列
            idName: 'id',  // 自定义id字段的名称
            pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
            isPidData: true,  // 是否是pid形式数据
            openName: 'spread'   // 自定义默认展开的字段名
          },
          cols: [
              {type: "numbers"},
              {type: "radio"},
              {field: 'title',  title: '部门名称'},
              /* {field: 'id', title: 'ID', align: "center"}, */
              {field: 'remark',  title: '部门备注', align: "center"},
              {field: 'address',  title: '部门地址', align: "center"},
          ],
          reqData: function(data, callback) {
            if(pid != null) {
              if(state === FORM_ADD) {
                // 在这里写ajax请求，通过callback方法回调数据
                $.get(api + '/dept/loadDept', function (res) {
                  callback(res.data);  
                });
              } else if(state === FORM_UPDATE) {
                $.get(api + '/dept/getDeptExcludeById', {'id': id}, function (res) {
                  callback(res.data); 
                  tableSelectIns.setChecked(pid);  // 设置选中数据 
                });
              }
            }
            else {
              $.get(api + '/dept/loadDept', function (res) {
                callback(res.data);  
              });
            }
          }
        });
      }
		});	
  });
}

function openMenuSelecter(state, id, pid, callBack) {
  layui.use(['layer','treeTable'], function () {
    var $ = layui.jquery;
    var treeTable = layui.treeTable;
    var layer=layui.layer;

    var tableSelectIns;
    selDeptIndex=layer.open({
			type:1,
			title:'选择部门',
			content:'<div style="padding:5px" ><table class="layui-hide" id="deptSelectTable" lay-filter="deptSelectTable"></table></div>',
			area:['700px','600px'],
			btn:['<span class="layui-icon layui-icon-ok"></span>确定'],
			yes:function(index){
				//得到选择的ID和名称
				var ckData=tableSelectIns.checkStatus();
				// console.log(ckData);
				callBack(ckData[0].id,ckData[0].title);
				layer.close(selDeptIndex);
			},
			success:function(index){
        tableSelectIns=treeTable.render({
          elem: '#deptSelectTable',
          cellMinWidth:true,
          tree: {
            iconIndex: 2,  // 折叠图标显示在第几列
            idName: 'id',  // 自定义id字段的名称
            pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
            isPidData: true,  // 是否是pid形式数据
            openName: 'spread'   // 自定义默认展开的字段名
          },
          cols: [
              {type: "numbers"},
              {type: "radio"},
              {field: 'title', title: '菜单名称'},
              {field: 'type',  title: '菜单位置', align: "center",width: 120,templet: function (d) {
                  if (d.type == 'topmenu') {
                      return '<span class="layui-badge layui-bg-red">顶部菜单</span>';
                  }
                  if (d.type == 'leftmenu') {
                      return '<span class="layui-badge layui-bg-blue">左侧菜单</span>';
                  }
                }
              },
              {field: 'typeCode', width: 120, title: '菜单编码'},
              {field: 'icon',  title: '菜单图标', align: "center",width: 120,templet:function(d){
                var x="<i class='"+d.icon+"' ></i>";
                return x;
              }}
          ],
          reqData: function(data, callback) {
            if(pid != null) {
              if(state === FORM_ADD) {
                // 在这里写ajax请求，通过callback方法回调数据
                $.get(api + '/menu/loadMenuList', function (res) {
                  callback(res.data);  
                });
              } else if(state === FORM_UPDATE) {
                $.get(api + '/menu/getMenuExcludeById', {'id': id}, function (res) {
                  callback(res.data); 
                  tableSelectIns.setChecked(pid);  // 设置选中数据 
                });
              }
            } else {
              $.get(api + '/menu/loadMenuList', function (res) {
                callback(res.data);  
              });
            }
          }
        });
      }
		});	
  });
}